package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.bean.SysUser;
import com.mall.bean.User;
import com.mall.service.UserService;
import com.mall.vo.UserVo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author little_super
 * @project hk_mall_background
 * @package com.mall.controller
 * @create 2017-09-02 0:33
 * //TODO something
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;

    @GetMapping("/getUserInfoList")
    @ResponseBody
    public Map<String,Object> getUserInfoList(@RequestParam(required=false,defaultValue = "1")int page,@RequestParam(required=false,defaultValue = "10")int limit){
        Map<String,Object> map = new HashMap<String, Object>();
        //ModelAndView modelAndView = new ModelAndView("user/userList");
        PageInfo<User> pageInfo = userService.getUserList(page,limit);
        List<User> list = pageInfo.getList();
        List<UserVo> listVo = new ArrayList<UserVo>();
        for (User user : list){
            String time = DateFormatUtils.format(user.getCreated(),"yyyy-MM-dd hh:mm:ss");
            UserVo userVo = new UserVo(user);
            listVo.add(userVo);
        }
        map.put("code",0);
        map.put("msg","");
        map.put("count",(int)pageInfo.getTotal());
        map.put("data",listVo);
        //modelAndView.addObject(map);
        return map;
    }

    @GetMapping("/gotoUserInfo")
    @ResponseBody
    public ModelAndView gotoUserInfo(){
        ModelAndView modelAndView = new ModelAndView("/user/userList");
        return modelAndView;
    }

    /**
     * 新增用户页面
     */
    @GetMapping("/getUserAdd")
    public String getUserAdd(){
        return "/user/userAdd";
    }


    /**
     * 新增用户
     */
    @PostMapping("/addUser")
    public String addUser(User user){
        userService.insertUser(user);
        return "/user/userAdd";
    }

    /**
     * 修改用户页面
     */
    @GetMapping("/getUserEdit")
    public ModelAndView getUserEdit(String id){
        Map<String,Object> map = new HashMap<String, Object>();
        User user  = userService.selectByUserId(id);
        System.out.println(user.toString());
        map.put("user",user);
        return new ModelAndView("/user/userEdit",map);
    }


    /**
     * 确认修改
     */
    @PostMapping("/userEdit")
    public String userEdit(User user){
        userService.updateByUserId(user);
        return "/user/userList";
    }


    /**
     * 根据ID删除用户信息
     */
    @GetMapping("/deleteById")
    public void deleteById(String id){
        userService.deleteById(id);
    }


    @GetMapping("/getUserTimeLine")
    public ModelAndView getUserTimeLine(){
        ModelAndView modelAndView = new ModelAndView("user/userTimeLine");
       /* List<User> list = userService.getUserList();
        ResponseBean bean = new ResponseBean(0,"",list.size(),list);
        modelAndView.addObject(bean);*/
        return modelAndView;
    }





}
